import config from '../config.js';
import { findJSFiles } from './findJSFiles.js';
import { getReactComponentsFromFiles } from './getReactComponentsFromFiles.js';
import path from 'path';

const isDev = process.env.NODE_ENV === 'development';
const currentDir = isDev ? path.resolve(process.cwd(), config.examplePath) : process.cwd();

/**
 * 从当前工作目录读取所有JSX/JS文件并提取React组件信息
 * @returns {Promise<Array>} React组件信息数组
 */
async function getReactComponentsFromCWD() {
  const cwd = currentDir;
  console.log(`正在从目录 ${cwd} 查找JSX/JS文件...`);
  const jsxFiles = await findJSFiles(cwd, /\.jsx?$/);
  console.log(`找到 ${jsxFiles.length} 个JSX/JS文件`);
  console.log('开始提取React组件信息...');
  const components = await getReactComponentsFromFiles(jsxFiles);
  console.log(`成功提取 ${components.length} 个React组件`);
  return components;
}

export { getReactComponentsFromCWD };